﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FitProtocolLibrary
{
    public class FitDefinitionMessage : FitMessage
    {
        public byte Reserved1;
        public byte Architecture;
        public ushort GlobalMessageNumber;
        public string Name;
        public byte Fields;
        public List<FitField> FieldDefinitions;

        public FitDefinitionMessage(FitMessage message, FitProfile profile, BinaryReader r) : base(message.Header.Header)
        {
            this.Reserved1 = r.ReadByte();
            this.Architecture = r.ReadByte();
            this.GlobalMessageNumber = r.ReadUInt16();
            
            this.Fields = r.ReadByte();
            this.FieldDefinitions = new List<FitField>();
            FitProfileMessage messagedef = profile.Messages.SingleOrDefault(q => q.Number == this.GlobalMessageNumber);
            this.Name = messagedef.Name;
            for (int i = 0; i < this.Fields; i++)
            {
                this.FieldDefinitions.Add(new FitField(messagedef, r.ReadBytes(3)));
            }
        }
    }
}
